package database

import (
	"fmt"

	"gitee.com/wangshh_123/cross-pay-go/src/models"
)

// InitData 初始化基础数据
func InitData() error {
	if err := initNotificationTypes(); err != nil {
		return err
	}
	if err := initCardFeeConfigs(); err != nil {
		return err
	}
	return nil
}

// 初始化通知类型
func initNotificationTypes() error {
	notificationTypes := []models.NotificationType{
		{
			Code:        "card_status_update",
			Name:        "卡片状态更新",
			Description: "卡片状态变更通知",
		},
		{
			Code:        "transaction",
			Name:        "交易通知",
			Description: "交易相关通知",
		},
		{
			Code:        "settlement",
			Name:        "结算通知",
			Description: "结算相关通知",
		},
		{
			Code:        "global_account",
			Name:        "账户通知",
			Description: "账户相关通知",
		},
		{
			Code:        "system",
			Name:        "系统通知",
			Description: "系统相关通知",
		},
	}

	for _, nt := range notificationTypes {
		if err := DB.FirstOrCreate(&nt, models.NotificationType{Code: nt.Code}).Error; err != nil {
			return fmt.Errorf("初始化通知类型失败: %v", err)
		}
	}

	return nil
}

// 初始化卡费配置
func initCardFeeConfigs() error {
	cardFeeConfigs := []models.CardFeeConfig{
		// 充值卡配置
		{
			CardType:       "recharge",
			CardFormFactor: "virtual_card",
			Currency:       "USD",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "virtual_card",
			Currency:       "EUR",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "virtual_card",
			Currency:       "GBP",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "virtual_card",
			Currency:       "CNH",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "physical_card",
			Currency:       "USD",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "physical_card",
			Currency:       "EUR",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "physical_card",
			Currency:       "GBP",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "recharge",
			CardFormFactor: "physical_card",
			Currency:       "CNH",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		// 共享卡配置
		{
			CardType:       "share",
			CardFormFactor: "virtual_card",
			Currency:       "USD",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "virtual_card",
			Currency:       "EUR",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "virtual_card",
			Currency:       "GBP",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "virtual_card",
			Currency:       "CNH",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "physical_card",
			Currency:       "USD",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "physical_card",
			Currency:       "EUR",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "physical_card",
			Currency:       "GBP",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
		{
			CardType:       "share",
			CardFormFactor: "physical_card",
			Currency:       "CNH",
			OpenCardFee:    0.00,
			MonthlyFee:     0.00,
			ExchangeRate:   1.000000,
			RefundRatio:    0.00,
			Status:         "active",
			CreatedBy:      1,
			UpdatedBy:      1,
		},
	}

	for _, config := range cardFeeConfigs {
		// 使用 FirstOrCreate 避免重复创建
		if err := DB.FirstOrCreate(&config, models.CardFeeConfig{
			CardType:       config.CardType,
			CardFormFactor: config.CardFormFactor,
			Currency:       config.Currency,
		}).Error; err != nil {
			return fmt.Errorf("初始化卡费配置失败: %v", err)
		}
	}

	return nil
}
